package cn.stylefeng.guns.schedule;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.thread.ThreadUtil;
import cn.hutool.core.util.StrUtil;
import cn.stylefeng.guns.base.utils.Ao81Model;
import cn.stylefeng.guns.base.utils.Ao81Util;
import cn.stylefeng.guns.base.utils.DingUtil;
import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.TimeUnit;

/**
 * @program: football_manage
 * @description:
 * @author: xiesm
 * @create: 2022-04-19 09:06
 **/
@Slf4j
//@Component
public class Ao86Schedule implements CommandLineRunner {
    @Override
    public void run(String... args) {
        new Thread(() -> {
            List<String> nums = Arrays.asList("4", "9");
            while (true) {
                String mm = DateUtil.format(new Date(), "mm").substring(1);
                if (nums.contains(mm)) {
                    Integer ss = Integer.valueOf(DateUtil.format(new Date(), "ss"));
                    test();
                    ThreadUtil.sleep(60 - ss, TimeUnit.SECONDS);
                } else {
                    ThreadUtil.sleep(60000);
                }
            }
        }).start();
    }

    public void test() {
        try {
            List<Ao81Model> datas = Ao81Util.parse();
            Ao81Model ao81Model = datas.get(0);
            printCode(ao81Model);
            for (int i = 1; i <= 8; i++) {
                find(i, ao81Model.getCode(i), datas);
                find2(i, ao81Model.getCode(i), datas);
//                find3(i, ao81Model.getCode(i), datas);
//                find4(i, ao81Model.getCode(i), datas);
                find5(i, ao81Model.getCode(i), 1, datas);
                find5(i, ao81Model.getCode(i), 2, datas);
            }
            cfu(datas, 3);
            cfu(datas, 4);
            cfu(datas, 6);
            cha(datas, 10);
            showCfuToday(datas);
            lianXucha(datas);
            sameCha(datas);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
    }

    //连续相同3
    public void cfu(List<Ao81Model> datas, int count) {
        String content = "";
        for (int i = 1; i < 9; i++) {
            List<Integer> list = Lists.newArrayList();
            for (int j = 0; j < count; j++) {
                Ao81Model data = datas.get(j);
                list.add(data.getCode(i));
            }
            content += search(list, datas, i);
        }
        if (StrUtil.isNotBlank(content)) {
            log.info(content);
            switch (count) {
                case 3:
                    DingUtil.send(content, "4");
                    break;
                case 4:
                    DingUtil.send(content, "5");
                    break;
                case 6:
                    DingUtil.send(content, "6");
                    break;
            }
        }
    }

    public String search(List<Integer> list, List<Ao81Model> datas, int wz) {
        String content = "";
        for (int i = 1; i < datas.size() - list.size(); i++) {
            for (int j = 1; j < 9; j++) {
                int num = 0;
                for (int k = 0; k < list.size(); k++) {
                    Ao81Model data = datas.get(i + k);
                    Integer code = data.getCode(j);
                    if (Objects.equals(list.get(k), code)) {
                        num++;
                    }
                }
                if (num == list.size()) {
                    StringBuffer sb = new StringBuffer();
                    sb.append(StrUtil.padPre(String.valueOf(datas.get(i - 1).getCode(j)), 2, "0"));
                    sb.append("|");
                    for (int k = 0; k < list.size(); k++) {
                        String code = String.valueOf(datas.get(i + k + 1 - 1).getCode(j));
                        code = StrUtil.padPre(code, 2, "0");
                        sb.append(code);
                        sb.append("|");
                    }
                    content += StrUtil.format("{}({})|issuse:{},issuse:{},【{}】\n",
                            wz,
                            getDateHHMM(),
                            datas.get(0).getIssue(),
                            datas.get(i).getIssue(),
                            sb.substring(0, sb.length() - 1));
                }
            }
        }
        return content;
    }

    public Integer getCode(int i, int j, List<Ao81Model> datas) {
        Integer code2 = 0;
        switch (i) {
            case 1:
                code2 = datas.get(j).getCode1();
                break;
            case 2:
                code2 = datas.get(j).getCode2();
                break;
            case 3:
                code2 = datas.get(j).getCode3();
                break;
            case 4:
                code2 = datas.get(j).getCode4();
                break;
            case 5:
                code2 = datas.get(j).getCode5();
                break;
            case 6:
                code2 = datas.get(j).getCode6();
                break;
            case 7:
                code2 = datas.get(j).getCode7();
                break;
            case 8:
                code2 = datas.get(j).getCode8();
                break;
        }
        return code2;
    }

    //重复3
    public void find(int i, Integer code, List<Ao81Model> datas) {
        Integer num = 0;
        for (int j = 1; j < 3; j++) {
            boolean boo = false;
            Integer code2 = getCode(i, j, datas);
            if (!Objects.equals(code, code2)) {
                boo = true;
            } else {
                num++;
            }
            if (boo) {
                break;
            }
            if (num == 2) {
                String content = StrUtil.format("{}({})|issuse:{},【{}】\n",
                        i,
                        getDateHHMM(),
                        datas.get(0).getIssue(),
                        StrUtil.padPre(code + "", 2, "0"));
                System.out.println(content);
                DingUtil.send(content, "2");
            }
        }
    }

    //重复2
    public void find2(int i, Integer code, List<Ao81Model> datas) {
        Integer num = 0;
        for (int j = 1; j < 2; j++) {
            Integer code2 = getCode(i, j, datas);
            if (Objects.equals(code, code2)) {
                num++;
            }
            if (num == 1) {
                String content = StrUtil.format("{}({})|issuse:{},【{}】\n",
                        i,
                        getDateHHMM(),
                        datas.get(0).getIssue(),
                        StrUtil.padPre(code + "", 2, "0"));
                System.out.println(content);
                DingUtil.send(content, "1");
            }
        }
    }

    //连续5-6
    public void find3(int i, Integer code, List<Ao81Model> datas) {
        Integer num = 0;
        for (int j = 1; j < 5; j++) {
            Ao81Model model = datas.get(j);
            for (int k = 1; k <= 8; k++) {
                Integer code2 = model.getCode(k);
                if (Objects.equals(code, code2)) {
                    num++;
                }
            }
        }
        if (num == 4) {
            String content = StrUtil.format("issue:{},\ndate:{},\ntype:1,\nnum:{},code:{}",
                    datas.get(0).getIssue(), getDateHHMM(), i, code);
            log.info(content);
            DingUtil.send(content, "2");

        }
    }

    //连续5-6 重号
    public void find4(int i, Integer code, List<Ao81Model> datas) {
        Integer num = 0;
        for (int j = 1; j < 6; j++) {
            Ao81Model model = datas.get(j);
            for (int k = 1; k <= 8; k++) {
                Integer code2 = model.getCode(k);
                if (Objects.equals(code, code2)) {
                    num++;
                }
            }
        }
        if (num == 5) {
            Integer code2 = datas.get(1).getCode(i);
            if (Objects.equals(code, code2)) {
                String content = StrUtil.format("issue:{},\ndate:{},\ntype:2,\nnum:{},code:{}",
                        datas.get(0).getIssue(), getDateHHMM(), i, code);
                log.info(content);
                DingUtil.send(content, "2");
            }

        }
    }

    //号码=漏
    public void find5(int i, Integer code, Integer beisu, List<Ao81Model> datas) {
        boolean boo = true;
        for (int j = 1; j < code * beisu; j++) {
            Ao81Model model = datas.get(j);
            Integer code2 = model.getCode(i);
            if (Objects.equals(code, code2)) {
                boo = false;
                break;
            }
        }
        if (boo) {
            Ao81Model model = datas.get(code * beisu + 1);
            Integer code2 = model.getCode(i);
            if (Objects.equals(code, code2)) {
                String content = StrUtil.format("{}({})|issuse:{},{}【{}】\n",
                        i,
                        getDateHHMM(),
                        datas.get(0).getIssue(),
                        beisu,
                        StrUtil.padPre(code + "", 2, "0"));
                System.out.println(content);
                DingUtil.send(content, "3");
            }
        }
    }

    //获取时间
    public String getDateHHMM() {
        return DateUtil.format(new Date(), "HH:mm");
    }

    //差10
    public void cha(List<Ao81Model> datas, int cha) {
        String content = "";
        for (int i = 1; i < 9; i++) {
            Integer code0 = datas.get(0).getCode(i);
            Integer code1 = datas.get(1).getCode(i);
            Integer code2 = datas.get(2).getCode(i);
            Integer code3 = datas.get(3).getCode(i);
            boolean boo01 = code1 > code0 ? (code0 + cha) == code1 : code0 == (code1 + cha);
            boolean boo23 = code3 > code2 ? (code2 + cha) == code3 : code2 == (code3 + cha);
            if (boo01 && boo23) {
                content += StrUtil.format("{}({})|issuse:{},【{}|{}|{}|{}】\n",
                        i,
                        getDateHHMM(),
                        datas.get(0).getIssue(),
                        StrUtil.padPre(code0 + "", 2, "0"),
                        StrUtil.padPre(code1 + "", 2, "0"),
                        StrUtil.padPre(code2 + "", 2, "0"),
                        StrUtil.padPre(code3 + "", 2, "0"));
            }
        }
        if (StrUtil.isNotBlank(content)) {
            System.out.println(content);
            switch (cha) {
                case 10:
                    DingUtil.send(content, "7");
                    break;
            }
        }
    }

    //连续差异
    public void lianXucha(List<Ao81Model> datas) {
        String content = "";
        for (int i = 1; i < 9; i++) {
            Integer code0 = datas.get(0).getCode(i);
            Integer code1 = datas.get(1).getCode(i);
            Integer code2 = datas.get(2).getCode(i);
            Integer code3 = datas.get(3).getCode(i);

            boolean boo = (code1 - (code3 - code2)) == code0;
            if (boo) {
                content += StrUtil.format("{}({})|issuse:{},【{}|{}|{}|{}】\n",
                        i,
                        getDateHHMM(),
                        datas.get(0).getIssue(),
                        StrUtil.padPre(code0 + "", 2, "0"),
                        StrUtil.padPre(code1 + "", 2, "0"),
                        StrUtil.padPre(code2 + "", 2, "0"),
                        StrUtil.padPre(code3 + "", 2, "0")
                );
            }
        }
        if (StrUtil.isNotBlank(content)) {
            System.out.println(content);
            DingUtil.send(content, "8");
        }
    }

    public void printCode(Ao81Model ao81Model) {
        String content = StrUtil.format("issue:{},({}),{}|{}|{}|{}|{}|{}|{}|{}",
                ao81Model.getIssue(),
                getDateHHMM(),
                StrUtil.padPre(ao81Model.getCode1() + "", 2, "0"),
                StrUtil.padPre(ao81Model.getCode2() + "", 2, "0"),
                StrUtil.padPre(ao81Model.getCode3() + "", 2, "0"),
                StrUtil.padPre(ao81Model.getCode4() + "", 2, "0"),
                StrUtil.padPre(ao81Model.getCode5() + "", 2, "0"),
                StrUtil.padPre(ao81Model.getCode6() + "", 2, "0"),
                StrUtil.padPre(ao81Model.getCode7() + "", 2, "0"),
                StrUtil.padPre(ao81Model.getCode8() + "", 2, "0"));
        log.info(content);
        DingUtil.send(content, "9");
    }

    public void printMsg(String content, String type) {
        if (StrUtil.isNotBlank(content)) {
            System.out.println(content);
            DingUtil.send(content, type);
        }
    }

    //展示今天重复3
    public void showCfuToday(List<Ao81Model> datas) {
        String dateStr = DateUtil.format(new Date(), "HH:mm");
        if (!"23:59".equals(dateStr)) {
            return;
        }
        String content = "";
        String beginIssue = datas.get(datas.size() - 1).getIssue();
        for (int i = 0; i < datas.size() - 3; i++) {
            for (int j = 1; j < 9; j++) {
                Integer code = datas.get(i).getCode(j);
                Integer num = 0;
                for (int k = 1; k < 3; k++) {
                    if (code == null) {
                        code = datas.get(i + k).getCode(j);
                        continue;
                    } else if (code == datas.get(i + k).getCode(j)) {
                        num++;
                    }
                }
                if (num == 2) {
                    DateTime date = DateUtil.offsetDay(new Date(), -1);
                    String str = DateUtil.format(date, "yyyy-MM-dd");
                    content += StrUtil.format("{}({})|issuse:{},【{}】\n",
                            j,
                            Ao81Util.getTimeFromIssue(Integer.valueOf(beginIssue),
                                    Integer.valueOf(datas.get(i).getIssue()), str),
                            datas.get(i).getIssue(),
                            StrUtil.padPre(code + "", 2, "0"));
                }
            }
        }
        if (StrUtil.isNotBlank(content)) {
            log.info(content);
            DingUtil.send(content, "2");
        }
    }

    //同位
    public void tongwei(List<Ao81Model> datas) {
        Ao81Model ao81Model = datas.get(1);
        String content = "";
        for (int i = 1; i < 9; i++) {
            Integer code = ao81Model.getCode(i);
            Integer num = datas.get(2).hasCode(code);
            if (num != -1) {
                Integer code1 = ao81Model.getCode(num + 1);
                Integer code2 = datas.get(0).getCode(i);
                if (code1 == code2) {
                    content += StrUtil.format("{}({})|issuse:{},【{}|{}|{}】",
                            i,
                            getDateHHMM(),
                            datas.get(0).getIssue(),
                            StrUtil.padPre(datas.get(0).getCode(num + 1) + "", 2, "0"),
                            StrUtil.padPre(code1 + "", 2, "0"),
                            StrUtil.padPre(code + "", 2, "0")
                    );
                }
            }
        }
        printMsg(content, "8");
    }

    //漏10
    public void lou10(List<Ao81Model> datas) {
        String content = "";
        for (int j = 1; j < 9; j++) {
            Integer num0 = datas.get(0).getCode(j);
            Integer num11 = datas.get(11).getCode(j);
            if (num0 != num11) {
                continue;
            }

            boolean boo = true;
            for (int k = 1; k < 11; k++) {
                Integer code = datas.get(k).getCode(j);
                if (code == num0) {
                    boo = false;
                    break;
                }
            }
            if (boo) {
                content += StrUtil.format("{}({})|issuse:{},【{}】\n",
                        j,
                        getDateHHMM(),
                        datas.get(0).getIssue(),
                        StrUtil.padPre(num0 + "", 2, "0")
                );
            }
        }

        if (StrUtil.isNotBlank(content)) {
            System.out.println(content);
        }
    }

    //相邻
    public void xiangLin(List<Ao81Model> datas) {
        String content = "";
        for (int i = 1; i < 8; i++) {
            Integer code01 = datas.get(0).getCode(i);
            Integer code12 = datas.get(1).getCode(i + 1);

            Integer code11 = datas.get(1).getCode(i);
            Integer code21 = datas.get(2).getCode(i + 1);
            boolean boo = Objects.equals(code12 - code01, code21 - code11);
            if (boo) {
                content += StrUtil.format("{}({})|issuse:{},【{}|{}|{}|{}】\n",
                        i,
                        getDateHHMM(),
                        datas.get(0).getIssue(),
                        StrUtil.padPre(code01 + "", 2, "0"),
                        StrUtil.padPre(code12 + "", 2, "0"),
                        StrUtil.padPre(code11 + "", 2, "0"),
                        StrUtil.padPre(code21 + "", 2, "0")
                );
            }
        }
        for (int i = 2; i < 9; i++) {
            Integer code01 = datas.get(0).getCode(i);
            Integer code12 = datas.get(1).getCode(i - 1);

            Integer code11 = datas.get(1).getCode(i);
            Integer code21 = datas.get(2).getCode(i - 1);
            boolean boo = Objects.equals(code12 - code01, code21 - code11);
            if (boo) {
                content += StrUtil.format("{}({})|issuse:{},【{}|{}|{}|{}】\n",
                        i,
                        getDateHHMM(),
                        datas.get(0).getIssue(),
                        StrUtil.padPre(code01 + "", 2, "0"),
                        StrUtil.padPre(code12 + "", 2, "0"),
                        StrUtil.padPre(code11 + "", 2, "0"),
                        StrUtil.padPre(code21 + "", 2, "0")
                );
            }
        }
        if (StrUtil.isNotBlank(content)) {
            System.out.println(content);
        }
    }

    //同位隔号差
    public void sameCha(List<Ao81Model> datas) {
        String content = "";
        Ao81Model ao81Model0 = datas.get(0);
        Ao81Model ao81Model1 = datas.get(1);
        Ao81Model ao81Model3 = datas.get(3);
        for (int i = 1; i < 9; i++) {
            Integer code0 = ao81Model0.getCode(i);
            Integer code1 = ao81Model1.getCode(i);
            Integer code = ao81Model3.getCode(i);
            for (int j = 5; j < datas.size(); j++) {
                Ao81Model ao81Model_1 = datas.get(j);
                Integer code_1 = ao81Model_1.getCode(i);
                if (Objects.equals(code, code_1)) {
                    Integer code2 = datas.get(j - 2).getCode(i);
                    Integer code3 = datas.get(j - 3).getCode(i);
                    if (Objects.equals(code2 - code3, code1 - code0)) {
                        content += StrUtil.format("{}({})|issuse:{},【{}|{}|{}】【{}|{}|{}】\n",
                                i,
                                getDateHHMM(),
                                datas.get(0).getIssue(),
                                StrUtil.padPre(code0 + "", 2, "0"),
                                StrUtil.padPre(code1 + "", 2, "0"),
                                StrUtil.padPre(code + "", 2, "0"),
                                StrUtil.padPre(code3 + "", 2, "0"),
                                StrUtil.padPre(code2 + "", 2, "0"),
                                StrUtil.padPre(code_1 + "", 2, "0")
                        );
                    }
                    break;
                }
            }
        }

        if (StrUtil.isNotBlank(content)) {
            DingUtil.send(content, "10");
        }
    }


    public static void main(String[] args) {
        Ao86Schedule schedule = new Ao86Schedule();
        List<Ao81Model> datas = Ao81Util.parse(0);
        schedule.sameCha(datas);
//        schedule.xiangLin(datas);
//        for (int i = 0; i < 22; i++) {
//            datas.remove(0);
//            System.out.println(datas.size());
//            for (int j = 1; j < 9; j++) {
//                schedule.find5(j, datas.get(0).getCode(j), 1, datas);
//                schedule.find5(j, datas.get(0).getCode(j), 2, datas);
//            }
//        }
//        datas.remove(0);
//        datas.remove(0);
//        datas.remove(0);
//        datas.remove(0);
//        datas.remove(0);
//        datas.remove(0);
//        datas.remove(0);
//        schedule.tongwei(datas);
        for (int i = 0; i < datas.size() - 10; i++) {
            datas.remove(0);
            schedule.sameCha(datas);
        }
    }

}
